

$(function () {
    $(".nav h2").on("click", function () {
    $(this).next().slideToggle();
    
})
    
})
window.onload=function(){
    let box = document.querySelector('.box');
    let right = document.querySelector('.right');
    box.onclick = function (e) {
        e = e || Event;
        if (e.target.classList.contains('souye')) {
            right.innerHTML = `
            <img src="../data/img/logo.jpg" alt="">
                <h1>欢迎来到宿舍管理系统</h1>
            `
        }
        if (e.target.classList.contains('sg')) {
            data();
        }
        if (e.target.classList.contains("student"))     {
            studentdata();
        }
        if (e.target.classList.contains('occupancy')) {
            fn();
        }
        if (e.target.classList.contains("quit")) {
            let flag = confirm("确认退出宿舍管理系统？");
            if (!flag) return;
            location.href = "../index.html";
        }
        if (e.target.classList.contains("record")) {
            record();
        }
        if (e.target.classList.contains("apply")) {
            apply();
        };
        if (e.target.classList.contains("gai")) {
            change();
        }
    }
    async function data() {
            let res = await pAjax({
                url: `/api/project/supervisordata.php`,
            });
            console.log(JSON.parse(res));
            res = JSON.parse(res);
            runden1(res);
            
        }
    function runden1(res) {
         $(".right").off("dblclick");
            let num= res.map(function (item) {
                return `
                    <tr data="${item.id}">
                                <td>${item.id}</td>
                                <td>${item.password}</td>
                                <td>${item.name}</td>
                                <td>${item.sex}</td>
                                <td>${item.tel}</td>
                                <td>${item.age}</td>
                                <td><button class="updata">修改</button><button class="del">删除</button></td>
                            </tr>
                `
                }).join('');
            right.innerHTML = `
            <div class="supervisor">
            <div class="supervisor_t">
                    <button class="add1">添加</button>
                    <label for="" class="lable1">查询方式：</label>
                    <select name="" id="select">
                        <option value="1">工号</option>
                        <option value="2">姓名</option>
                        <option value="3">性别</option>
                    </select>
                    <input type="text" class="shuru">
                    <button class="chaxun">查询</button>
                    <button class="all">查看全部</button>


                </div>
                <div class="adddata1">
                    <div class="zzc">
                        <form action="" id="superform">
                            <p>新增宿管</p>
                            <p class="yz">
                                <label for="">工号：</label>
                                <input type="text" placeholder="请输入工号" name="user" id="user">
                            </p>
                            <p class="yz">
                                <label for="">密码：</label>
                                <input type="password" placeholder="请输入密码" name="password" id="password">
                            </p>
                            <p class="yz">
                                <label for="">姓名：</label>
                                <input type="text" placeholder="请输入真实姓名" name="namex" id="namex">
                            </p>
                            <p>
                                <label for="">性别：</label>
                                <input type="radio" checked value="男" name="idx"><label for="">男</label>
                                <input type="radio" value="女" name="idx"><label for="">女</label>
                            </p>
                            <p class="yz">
                                <label for="">电话：</label>
                                <input type="text" placeholder="请输入联系电话" name="tel" id="tel">
                            </p>
                            <p class="yz">
                                <label for="">年龄：</label>
                                <input type="text" placeholder="请输入年龄" name="age" id="age">
                            </p>
                            <p>
                                <button>添加</button>
                                <span class="cancel">取消</span>
                                <!-- <input type="submit" value=" 添加"> -->
                            </p>
                        </form>
                    </div>
                </div>
                <div class="supervisor_f">
                    <table>
                        <thead>
                            <tr>
                                <th>工号</th>
                                <th>密码</th>
                                <th>姓名</th>
                                <th>性别</th>
                                <th>联系电话</th>
                                <th>年龄</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${num}
                        </tbody>
                        <tfoot></tfoot>
                    </table>
                </div>
                </div>
                `
            $('.add1').on("click", function () {
        $('.adddata1').show();
        $('#superform').animate({
            left: ($(".right").width() - $("#superform").width()) / 2,
            top: ($(".right").height() - $("#superform").height()) / 2
        }, 0)
        
    
        });
        $('.cancel').on("click", function () {
        $('input').val('');
        $('.adddata1').hide();
        })
        jQuery.validator.addMethod(
                "telTest",
                function (value) {
                    let reg = /^1[3-9]\d{9}$/;
                    if (reg.test(value)) {
                        return true;
                    }
                    return false;
                },
                "手机号不正确"
        );
        jQuery.validator.addMethod(
                "passTest",
                function (value) {
                   
                    let reg = /^[0-9a-z]+$/i;
                    if (reg.test(value)) {
                        return true;
                    }
                    return false;
                },
                "密码只能由数字,字母组成"
        );
        jQuery.validator.addMethod(
                "userTest",
                function (value) {
                    let reg = /^3\d{3}$/i;
                    if (reg.test(value)) {
                        return true;
                    }
                    return false;
                },
                "工号由已3开头的四位数子组成"
        );
        jQuery.validator.addMethod(
                "nameTestx",
                function (value) {
                    let reg = /^[\u4e00-\u9fa5]{2,4}$/i;
                    if (reg.test(value)) {
                        return true;
                    }
                    return false;
                },
                "请输入真实姓名"
                );
        $("#superform").validate({
            rules: {
                user: {
                    required: true,
                    userTest: true
                },
                password: {
                    required: true,
                    passTest: true,
                    rangelength: [6, 12]
                },
                namex: {
                    required: true,
                    nameTestx: true
                },
                tel: {
                    required: true,
                    telTest: true
                },
                age: {
                    required: true,
                },
            },
            submitHandler: function () {
                let rex = $("input[name='idx']:checked").val();
                let user = $("#user").val();
                let password = $("#password").val();
                let namex = $("#namex").val();
                let tel = $("#tel").val();
                let age = $("#age").val();

                async function fun() {
                    let res = await pAjax({
                        url: `/api/project/supervisoradd.php`,
                        data: {
                            username: user,
                            password: password,
                            name: namex,
                            sex: rex,
                            tel: tel,
                            age: age
                        },
                    });
                    console.log(res);
                    res = JSON.parse(res);
                    if (res.code == 1) {
                        data();
                    } else {
                        alert("该工号已经存在，请重新添加！")
                    }
            
            
                }
                fun()
                $('input').val('');
                $('.adddata1').hide();
            },
        });

        $(".supervisor_t .chaxun").on("click", function () {
            let way = $("#select").val();
            let text = $(".supervisor_t .shuru").val();
            async function select() {
                let res = await pAjax({
                    url: `/api/project/supervisorselect.php`,
                    data: {
                        way: way,
                        text:text
                    },
                    type:"post",
                })
                console.log(res);
                res = JSON.parse(res);
                runden1(res);
            }
            select();
        })
        $(".supervisor_t .all").on("click", function () {
            data();
        })


        $("tbody .del").on("click", function () {
            let idx = $(this).parent().parent().attr('data');
            let flag = confirm("确认删除？")
            if (!flag) {
                return;
            }
            ajax({
                url: `/api/project/supervisordel.php`,
                data:{id:idx},
                success: function (res) {
                    if (JSON.parse(res).code == true) {
                        data()
                    }
                }
            })
        })
        $("tbody .updata").on("click", function () {
            $(this).parent().prevUntil("td:first-child").attr("contenteditable", "true");
            
            if ($(this).html() == "确认") {
                let upda = confirm("确认修改");
                if (upda == true) {
                    let text = $(this).parent().siblings();
                    let idx = text.eq(0).html();
                    let password = text.eq(1).html();
                    let namex = text.eq(2).html();
                    let sex = text.eq(3).html();
                    let tel = text.eq(4).html();
                    let age = text.eq(5).html();
                
                ajax({
                url: `/api/project/supervisorupdata.php`,
                    data: {
                        id: idx,
                        password:password,
                        namex:namex,
                        sex: sex,
                        tel: tel,
                        age:age
                    },
                success: function (res) {
                    if (JSON.parse(res).code == true) {
                        alert("数据更新成功");
                    }
                    
                }
            })
                } else {
                     data();
                }

                $(this).html("修改");
                $(this).parent().prevUntil("td:first-child").removeAttr("contenteditable");
                return
            }
            $(this).html("确认");
        })
        
    }
    async function studentdata() {
            let res = await pAjax({
                url: `/api/project/studentdata.php`,
            });
            localStorage.setItem("data",res)
            console.log(JSON.parse(res));
     res = JSON.parse(res);
     let index = 1;
            runden2(res,index);
            
        }
    function runden2(res, index) {
        
        let num = res.list.map(function (item, index) {
            if (index > 16) return;
                return `
                    <tr data="${item.id}">
                                <td>${item.id}</td>
                                <td>${item.password}</td>
                                <td>${item.name}</td>
                                <td>${item.sex}</td>
                                <td>${item.department}</td>
                                <td>${item.majo}</td>
                                <td>${item.class}</td>
                                <td>${item.dormitoryid}</td>
                            </tr>
                `
                }).join('');
        right.innerHTML = `
            <div class="supervisor">
            <div class="supervisor_t">
                    <button class="add1">添加</button>
                    <label for="" class="lable1">查询方式：</label>
                    <select name="" id="select">
                        <option value="1">学号</option>
                        <option value="2">姓名</option>
                        <option value="3">性别</option>
                        <option value="4">所属学院</option>
                        <option value="5">所属专业</option>
                        <option value="6">所属班级</option>
                        <option value="7">寝室号</option>
                        
                    </select>
                    <input type="text" class="shuru">
                    <button class="chaxun">查询</button>
                    <button class="all">查看全部</button>


                </div>
                <div class="adddata2">
                    <div class="zzc">
                        <form action="" id="studentform">
                            <p>新增学生信息</p>
                            <p class="yz">
                                <label for="">学号：</label>
                                <input type="text" placeholder="请输入学号" name="user" id="userid">
                            </p>
                            <p class="yz">
                                <label for="">密码：</label>
                                <input type="password" placeholder="请输入密码" name="password" id="password">
                            </p>
                            <p class="yz">
                                <label for="">姓名：</label>
                                <input type="text" placeholder="请输入真实姓名" name="namex" id="namex">
                            </p>
                            <p>
                                <label for="" class="sexx" >性别：</label>
                                <input type="radio" checked value="男" name="idx"><label for="">男</label>
                                <input type="radio" value="女" name="idx"><label for="">女</label>
                            </p>
                            <p class="yz">
                                <label for="">所属学院：</label>
                                <input type="text" placeholder="请输入所属学院" name="tel" id="dapartment">
                            </p>
                            <p class="yz">
                                <label for="">所属专业：</label>
                                <input type="text" placeholder="请输入所属电话" name="age" id="majo">
                            </p>
                            <p class="yz">
                                <label for="">所属班级：</label>
                                <input type="text" placeholder="请输入所属班级" name="age" id="class">
                            </p>
                            <p class="yz">
                                <label for="">寝室号：</label>
                                <input type="text" placeholder="请输入寝室号" name="age" id="dormitoryid">
                            </p>
                            <p>
                                <button id="studentadd">添加</button>
                                <span class="cancel">取消</span>
                        </form>
                    </div>
                </div>
                <div class="supervisor_f2">
                    <table>
                        <thead>
                            <tr>
                                <th>学号</th>
                                <th>密码</th>
                                <th>姓名</th>
                                <th>性别</th>
                                <th>所属学院</th>
                                <th>所属专业</th>
                                <th>班级</th>
                                <th>寝室号</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${num}
                        </tbody>
                        <tfoot></tfoot>
                    </table>
                    <div class="page"></div>
                </div>
                <div class="xiugai">
                    <div class="yy">
                        <table>
                            <thead>
                            <tr>
                                <th>学号</th>
                                <th>密码</th>
                                <th>姓名</th>
                                <th>性别</th>
                                <th>所属院校</th>
                                <th>所属专业</th>
                                <th>所属班级</th>
                                <th>宿舍号</th>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                            <tfoot></tfoot>
                            <div class="btns">
                                <button class="updata">修改信息</button><button class="del">删除该信息</button><button class="calen">取消</button>
                            </div>
                        </table>
                        
                    </div>
                </div>
                
            </div>
                `;
        
        let page = document.querySelector('.right .page');
             new Pagination(page, {
            pageInfo: {
                pagenum: index,
                pagesize: 17,
                total: res.total,
                totalpage: Math.ceil(res.total / 17)
            },
            textInfo: {
                first: '首页',
                prev: '上一页',
                next: '下一页',
                last: '末页'
            },
            // 当我们点击的时候 执行的函数
            change: function (index) {
                // if (index != 1 && index != 100) {
                //     $("html,body").animate({ scrollTop: 0 }, 500);
                // }
                let data = localStorage.getItem('data');
                data = JSON.parse(data);
                let shuju = data.list.slice((index - 1) * 17);
                console.log(shuju);
                data.list = shuju;
                console.log(data);
                runden2(data,index);

                // index 表示当前的页数


            }
             });
        if (res.total <= 17) {
            console.log(1);
            $(".right .page").hide();
            console.log( $(".right .page"));
        }
        
        
        $('.add1').on("click", function () {
        $('.adddata2').show();
        $('#studentform').animate({
            left: ($(".right").width() - $("#studentform").width()) / 2,
            top: ($(".right").height() - $("#studentform").height()) / 2
        }, 0)
        
    
        });
        $('.cancel').on("click", function () {
        $('input').val('');
        $('.adddata2').hide();
        })
        $('#studentadd').on("click", function (e) {
            e.preventDefault();
            console.log(1);
                let rex = $("input[name='idx']:checked").val();
                let userid = $("#userid").val();
                let password = $("#password").val();
                let namex = $("#namex").val();
                let dapartment = $("#dapartment").val();
                let majo = $("#majo").val();
                let classx = $("#class").val();
                let dormitoryid = $("#dormitoryid").val();
            ajax({
                    url: `/api/project/dormdata.php`,
                success: function (num) {
                    num = JSON.parse(num);
                    console.log(num);
                               num.forEach(function (item) {
                                if (dormitoryid == item.user) {
                                    let shu = item.live;
                                    if (shu == 6) {
                                        alert("该宿舍已住满，请重新安排宿舍");
                                        return;
                                    }
                                    shu++;
                                    ajax({
                                    url: `/api/project/studentadd.php`,
                                    data: {
                                                username: userid,
                                                password: password,
                                                name: namex,
                                                sex: rex,
                                                dapartment: dapartment,
                                                majo: majo,
                                                classx: classx,
                                                dormitoryid,dormitoryid
                                    },
                                

                                            success: function (res) {
                                                if (JSON.parse(res).code == 1) {
                                                    studentdata();
                                                    $('input').val('');
                                                    $('.adddata2').hide();
                                                }                 

                                            }
                                    })
                            } 
                }) 
                     
                            
                     
            }
        })
            
            
        })
        $(".supervisor_t .chaxun").on("click", function () {
            console.log(1);
            let way = $("#select").val();
            let text = $(".supervisor_t .shuru").val();
            async function select() {
                let res = await pAjax({
                    url: `/api/project/studentselect.php`,
                    data: {
                        way: way,
                        text:text
                    },
                    type:"post",
                })
                res = JSON.parse(res);
                runden2(res);
            }
            select();
        })
        $(".supervisor_t .all").on("click", function () {
            studentdata();
        })
        $(".right").on("dblclick", function (e) {
            if (e.target.nodeName == "TD") {
               
                $(e.target).parent().css("background-color", "red");
                $(e.target).parent().siblings().css("background-color", "");
                let text=e.target.parentNode.innerHTML
                let tr = $('<tr></tr>');
                tr.html(text);
                console.log($(".xiugai"));
                $(".xiugai").show();
                $(".xiugai tbody").html('').append(tr);
               
            }
             $(".xiugai .btns .calen").on("click", ()=>$(".xiugai").hide())
            $(".xiugai .btns .del").on("click", function () {
                let id = $(".xiugai .yy td:first-child").html();
                let flag = confirm("是否删除该数据？");
                if (!flag) return;
                ajax({
                    url: "/api/project/studentdel.php",
                    data: { id: id },
                    success: function (res) {
                        console.log(res);
                        res = JSON.parse(res);
                        if (res.code == true) {
                            alert("删除数据成功");
                            studentdata();
                            $(".xiugai").hide();

                        } else {
                            alert("删除数据失败")
                        }
                    }
                })
            })
            $(".xiugai .btns .updata").on("click", function () {
                let qsx = $(".xiugai .yy td:nth-child(8)").html();
                $(".xiugai .yy td").attr("contenteditable", "true");
                if ($(this).html() == "确认修改") {
                    let flag = confirm("是否确认修改?");
                    if (flag == false) return;
                        let id = $(".xiugai .yy td:first-child").html();
                        let password = $(".xiugai .yy td:nth-child(2)").html(); 
                        let namea = $(".xiugai .yy td:nth-child(3)").html();
                        let sex = $(".xiugai .yy td:nth-child(4)").html();
                        let yx = $(".xiugai .yy td:nth-child(5)").html();
                        let zy = $(".xiugai .yy td:nth-child(6)").html();
                        let bj = $(".xiugai .yy td:nth-child(7)").html();
                    let qs = $(".xiugai .yy td:nth-child(8)").html();
                    ajax({
                        url: `/api/project/studentupdata.php`,
                        data: {
                            id: id,
                            password: password,
                            namea: namea,
                            sex: sex,
                            yx: yx,
                            zy: zy,
                            bj: bj,
                            qs:qs
                        },
                        success: function (res) {
                            res = JSON.parse(res);
                            if (res.code == true) {
                                studentdata()
                                $(".xiugai").hide()
                            }
                            if (qsx == qs) return;


                        }
                    })
                    
                }
                $(this).html("确认修改");
            })
        })
    }
    async function fn() {
        let res2 = await pAjax({
                url: `/api/project/studentdata.php`,
            });
            console.log(JSON.parse(res2));
            res = JSON.parse(res2);
            dorm(res);
    }
    async function dorm(res2) {
        let res = await pAjax({
                url: `/api/project/dormdata.php`,
            });
            console.log(JSON.parse(res));
            res = JSON.parse(res);
            runden4(res,res2);
        
    }
    function runden4(res,res2) {



        $(".right").off("dblclick");
        $(".box .right").html(`
        <div class="tower">
                <div class="list">
                    <ul class="list_left">
                        <h3>楼宇1</h3>
                        <li>
                            <table>
                                <thead>
                                    <tr>
                                        <th>门牌号</th>
                                        <th>容量</th>
                                        <th>已住</th>
                                        <th>状态</th>
                                    </tr>
                                </thead>
                                <tbody>
                    
                                </tbody>
                                <tfoot></tfoot>
                            </table>
                            
                        </li>
                    </ul>
                    <ul class="list_right">
                        <h3>楼宇2</h3>
                        <li>
                            <table>
                                <thead>
                                    <tr>
                                        <th>门牌号</th>
                                        <th>容量</th>
                                        <th>已住</th>
                                        <th>状态</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    
                                </tbody>
                                <tfoot></tfoot>
                            </table>
                        </li>
                    </ul>
                    <ul class="list_tc">
                        

                    </ul>
                </div>
            </div>
        `)

        ;
        res.forEach(function (item) {
            let live = 0;

            res2.list.forEach(function (itm) {
                 if (item.user == itm.dormitoryid) {
                    live++;
                }
            })

            ajax({
                url: `/api/project/dormupdata.php`,
                data: {
                    user: item.user,
                    live:live
                },
                success: function (ress) {
                    console.log(ress);
                }
            })

            if (item.user.startsWith('1')) {
                let tr1 = $('<tr></tr>');
                let list = `
                <td>${item.user}</td>
                <td>${item.capacity}人</td>
                <td>${live}人</td>
                <td  data="${item.user}"><button class="${item.capacity<=live?"zt":''}">${item.capacity<=live?"已满":"未满"}</button><button class="look">查看</button></td>
                `
                if ($(".tower tbody .zt").html() == "已满") {
                    $(this).css("background-color", "blue");
                 }    
                tr1.html(list);
                $(".tower .list_left tbody").append(tr1);
                
           

            } else if (item.user.startsWith('2')) {
                let tr2 = $('<tr></tr>');
                let list2 = `
                <td>${item.user}</td>
                <td>${item.capacity}人</td>
                <td>${live}人</td>
                <td  data="${item.user}"><button class="${item.capacity<=live?"zt":''}">${item.capacity<=live?"已满":"未满"}</button><button class="look">查看</button></td>
                `
                tr2.html(list2);
                console.log(tr2);
                $(".tower .list_right tbody").append(tr2);
            }
            
            

        })
        $(".tower .list .look").on("click", function () {
            let id = $(this).parent().attr("data");
            dormup(id);
            })

            async function dormup(id) {
                let res = await pAjax({
                    url: `/api/project/studentdata.php`,
                })
                res = JSON.parse(res);
                rundent5(res,id)
            }
            
                
        function rundent5(res, id) {
                    let nux = '';
                    res.list.forEach((item) => { 
                        if (item.dormitoryid ==id) {
                            nux += `
                             <tr>
                                <td>${item.id}</td>
                                <td>${item.name}</td>
                                <td>${item.department}</td>
                                <td>${item.majo}</td>
                                <td>${item.dormitoryid}</td>
                                <td><button class="up">修改</button>
                                <button class="yc">移除</button></td>
                                </tr>
                            `;
                            
                        }
                    });
                    
                    $(".list_tc").html(`<div class="zc">
                            <h3>${id}室</h3>
                            <li>
                                <table>
                                    <thead>
                                        <tr>
                                            <th>学号</th>
                                            <th>姓名</th>
                                            <th>所属学院</th>
                                            <th>专业</th>
                                            <th>宿舍号</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        ${nux}
                                    </tbody>
                                    <tfoot></tfoot>
                                </table>
                            </li>
                            <button class="gb">关闭刷新</button>
                        </div>`);
                    $(".list_tc").show();
                    $(".list_tc .zc").animate({
                        left: ($(".right").width() - $(".list_tc .zc").width()) / 2,
                        top: ($(".right").height() - $(".list_tc .zc").height()) / 2
                    }, 0);
                     $(".list_tc .gb").on("click", function () {
                         $(".list_tc").hide();
                         fn();
                     })
                    $(".list_tc .yc").on("click", function () {
                        // let id = $(this).parent().prev().html();
                        let flag = confirm("该操作会删除该学生的所有信息，是否继续执行？");
                        if (!flag) return;
                        let idx = $(this).parent().prevAll().eq(4).html();
                        let idxx = $(this).parent().prevAll().eq(0).html();
                        ajax({
                        url: "/api/project/studentdel.php",
                        data: { id: idx },
                        success: function (res) {
                        console.log(res);
                        res = JSON.parse(res);
                        if (res.code == true) {
                            alert("删除数据成功");
                            dormup(idxx);
                        } else {
                            alert("删除数据失败")
                        }
                    }
                    })

                    })
            $(".list_tc .up").on("click", function () {
                let zq = $(this).parentsUntil("div").eq(4).prev().html();
                zq=zq.substr(0,5)
                $(this).addClass('queren')
                        

                        $(this).parent().prev().attr("contenteditable", "true");
                        
                        if ($(this).html() == "确认") {
                            $(this).parent().prev().removeAttr("contenteditable");
                            let bs = confirm("是否确认修改");
                            if (!bs) return;
                            let hao2 = $(this).parent().prev().html();
                            let idx = $(this).parent().prevAll().eq(4).html();
                            ajax({
                                url: `/api/project/dormup.php`,
                                data: {
                                    id: idx,
                                    qs:hao2
                                },
                                success: function (res) {
                                    dormup(zq);
                                }
                            })
                        }
                        $(this).html("确认")
                        
                    })

                } 
    }

    async function record() {
        let res = await pAjax({
            url: `/api/project/recodedata.php`,
        })
        res = JSON.parse(res);
        runden6(res);
    }
    function runden6(res) {
        let num= res.map(function (item) {
                return `
                    <tr data="${item.id}">
                                <td>${item.menhao}</td>
                                <td>${item.user}</td>
                                <td>${item.description}</td>
                                <td>${item.newtime}</td>
                                <td>${item.oldtime==null?"":item.oldtime}</td>
                                <td>${item.state}</td>
                                <td>${item.remark==null?"":item.remark}</td>
                            </tr>
                `
                }).join('');
        right.innerHTML = `
        <div class="supervisor weixiu">
            <div class="supervisor_t">
                    <label for="" class="lable1">查询方式：</label>
                    <select name="" id="select">
                        <option value="1">寝室号</option>
                        <option value="2">申请时间</option>
                        <option value="3">维修时间</option>
                        <option value="4">状态查询</option>
                    </select>
                    <input type="text" class="shuru">
                    <button class="chaxun">查询</button>
                    <button class="all">查看全部</button>


                </div>
         <div class="supervisor_f2">
                    <table>
                        <thead>
                            <tr>
                                <th>寝室</th>
                                <th>学号</th>
                                <th>维修描述</th>
                                <th>申请时间</th>
                                <th>维修时间</th>
                                <th>当前状态</th>
                                <th>备注</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${num}
                        </tbody>
                        <tfoot></tfoot>
                    </table>
                </div>
            </div>
        `
        $(".weixiu .chaxun").on("click", function () {
            console.log(111);
            let way = $("#select").val();
            let text = $(".weixiu .shuru").val();
            console.log(way);
            console.log(text);
            async function select() {
                let res = await pAjax({
                    url: `/api/project/recodeselect.php`,
                    data: {
                        way: way,
                        text:text
                    },
                    type:"post",
                })
                res = JSON.parse(res);
                runden6(res);
            }
            select()
        })
        $(".weixiu .all").on("click", function () {
            record();
        })
    }

    async function apply() {
        let res = await pAjax({
            url: `/api/project/recodedata.php`,
        })
        res = JSON.parse(res);
        runden7(res);
    }

    function runden7(res) {
        let nux = '';
        res.forEach(function (item) {
            console.log(item);
            if (item.state =="申请" || item.state =="受理") {
                
                 nux+= `
                    <tr data="${item.id}">
                                <td>${item.menhao}</td>
                                <td>${item.user}</td>
                                <td>${item.description}</td>
                                <td>${item.newtime}</td>
                                <td>${item.oldtime==null?"":item.oldtime}</td>
                                <td><button id="zz" class="${item.state=="申请"?"sq":"sl"}">${item.state}</button><button class="bh">驳回</button></td>
                                <td>${item.remark==null?"":item.remark}</td>
                            </tr>
                `
            }
                
        });
        right.innerHTML = `
        <div class="supervisor weixiu">
            <div class="supervisor_t">
                    <label for="" class="lable1">查询方式：</label>
                    <select name="" id="select">
                        <option value="1">寝室号</option>
                        <option value="2">申请时间</option>
                        <option value="4">状态查询</option>
                    </select>
                    <input type="text" class="shuru">
                    <button class="chaxun">查询</button>
                    <button class="all">查看全部</button>


                </div>
         <div class="supervisor_f2">
                    <table>
                        <thead>
                            <tr>
                                <th>寝室</th>
                                <th>学号</th>
                                <th>维修描述</th>
                                <th>申请时间</th>
                                <th>维修时间</th>
                                <th>当前状态</th>
                                <th>备注</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${nux}
                        </tbody>
                        <tfoot></tfoot>
                    </table>
                </div>
            </div>
        `

        
        $(".weixiu .chaxun").on("click", function () {
            console.log(111);
            let way = $("#select").val();
            let text = $(".weixiu .shuru").val();
             if (text == '') {
                alert("请输入关键字进行查询");
                return;
            }
            async function select() {
                let res = await pAjax({
                    url: `/api/project/recodeselect.php`,
                    data: {
                        way: way,
                        text:text
                    },
                    type:"post",
                })
                res = JSON.parse(res);
                runden7(res);
            }
            select()
        })
        $(".weixiu .all").on("click", function () {
            apply();
        })

        $(".weixiu tbody #zz").on("click", function () {
            let id = $(this).parent().parent().attr("data");
            if ($(this).html() == "申请") {
                let flag = confirm("是否受理该条维修记录");
                let text = prompt("请输入维修时间");
                if (!flag) return;
                async function sq() {
                    let res = await pAjax({
                        url: `/api/project/recodeupdata.php`,
                        data: {
                            id: id,
                            zt: "受理",
                            time: "",
                            text:text
                            
                        }
                    })
                    console.log(res);
                    apply()
                }
                sq();
                
            }
            if ($(this).html() =="受理") {
                let flag = confirm("是否已完成该维修");
                if (!flag) return;
                let data = new Date();
                let time=formatDate(data);
                time = `${time.year}-${time.month}-${time.date}`;
                
                async function sl() {
                    let res = await pAjax({
                        url: `/api/project/recodeupdata.php`,
                        data: {
                            id: id,
                            zt: "完成",
                            time: time,
                            text:""
                        }
                    })
                    apply();
                    console.log(res);
                }
                sl()
            }
        })
        $(".weixiu tbody .bh").on("click", function () {
            let flag = confirm("确认驳回该申请？");
            if (!flag) return;
            let text = prompt("请输入驳回理由");
            let id = $(this).parent().parent().attr("data");
            async function bh() {
                    let res = await pAjax({
                        url: `/api/project/recodeupdata.php`,
                        data: {
                            id: id,
                            zt: "驳回",
                            time: "",
                            text:text
                            
                        }
                    })
                    console.log(res);
                    apply()
                }
                bh();
            
        })
        function formatDate(d) {
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        month = month < 10 ? "0" + month : month;

        var date = d.getDate();
        date = date < 10 ? "0" + date : date;


    return {
        year: year,
        month: month,
        date: date,
    };
}
    }

    function change() {
        $(".box .mima").show();
        $('#mi').animate({
            left: ($(".right").width() - $("#mi").width()) / 2,
            top: ($(".right").height() - $("#mi").height()) / 2
        }, 0);
        jQuery.validator.addMethod(
                "userTestx",
                function (value) {
                    let user = getCookie("login");
                    if (value == user) {
                        return true;
                    } else {
                        return false;
                    }
                },
                "用户名不正确"
        );
        jQuery.validator.addMethod(
                "passTest",
            function (value) {
                let flag = true;
                ajax({
                    url: `/api/project/admindata.php`,
                    async: false,
                    success: function (res) {
                        let user = getCookie("login");
                        res = JSON.parse(res);
                        res.forEach(function (item) {
                                // if (item.username == user) {
                                    // console.log(item.password);
                                    // console.log(value);
                                 if (item.username == user&&item.password == value) {
                                        // console.log(1);
                                    flag = true;
                                 } else {
                                    //  console.log(2);
                                     flag = false;
                                    }
                                // }
                    });   
                    }
                })
                console.log(flag);
                return flag;




                },
                "原密码不正确"
                );


         $("#mi").validate({
            rules: {
                user: {
                    required: true,
                    userTestx:true
                },
                old: {
                    required: true,
                    passTest:true
                    
                },
                new: {
                    required: true,

                },
                agein: {
                    required: true,
                    equalTo:"#new"
 
                },
                
             },
             messages: {
                 agein: {
                     equalTo:"两次密码不一致"
                 }

             },
             submitHandler: function () {
                 let user = getCookie("login");
                 let password = $(".box .mima #mi #new").val();
                 console.log(password);
                async function tj() {
                    let res = await pAjax({
                        url: `/api/project/adminupdata.php`,
                        data: {
                            user: user,
                            password:password
                        },
                    })
                    console.log(res);
                    res = JSON.parse(res);
                    if (res.code == true) {
                        alert("修改密码成功");
                        $(".box .mima").hide();
                        location.href = "../html/index.html";
                    }
                 }
                 tj();
            },
         });
        
        
        $(".box .mima #cel").on("click", function () {
            $(".box .mima").hide();
        })
    }
}
